﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using AOT;
using System.Runtime.InteropServices;
using MessageSystem.Managed.Rpc;
using Lua4Net;
using MessageSystem.Managed.LuaRpc;
using MessageSystem.Managed.Infrastructure;
using MessageSystem.Managed;

	[Scriptable]
	public class MainHero : MonoBehaviour 
	{
		public static RpcCenter rpc;
		public static RpcAddress address;
		public static RpcPermanentDomain pdomain;
		
		//public Animator _anim{ get; set; }
		
		private LuaManager lmanager = new LuaManager();
		
		//Update is called once per frame
		void Update () {
			pdomain.Update();
			//!!!!!!!!must care this will Call instance lock....
			lmanager.GC();
			rpc.Poll(10);
		}
		
		[Scriptable(Name = "sayHello")]
		public void sayHello(){
			UnityEngine.Debug.Log ("Say Hello");
		}

		public void FireAttack()
	    {
			UnityEngine.Debug.Log ("Fire Attack");
		}
		
		// Use this for initialization
		void Start () 
		{
			List<LuaRegister> rglist = new List<LuaRegister>();
			rglist.Add(new Lua4NetRoot());
			rglist.Add(new LuaRpcRegister());
			lmanager.Initialize(rglist,Application.dataPath + "/luascripts/Generated/Lua4NetRoot.lua",Application.dataPath +"/luascripts/application.lua",
				                    s => { UnityEngine.Debug.Log(s); });
			
			rpc = new RpcCenter(Options.Default, MessageSystemType.Default);
			address = new RpcAddress { Ip = "192.168.1.122", Port = 8001 };
			pdomain = rpc.GetPermanentDomain(address, 3, 10);
			lmanager.Call("gamestart", pdomain, 0);
			lmanager.Call("fire",this);
		}
	}

